﻿Imports System.Data

Partial Class ArticlesHome
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            PopulateCloud()
        End If
    End Sub
    Private Sub PopulateCloud()
        CloudMarkup.Text = String.Empty

        Dim cloudData As DataView = CType(DSArticleTagCloud.Select(DataSourceSelectArguments.Empty), DataView)

        Const SpacerMarkup As String = " "
        Dim FontScale() As String = {"xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"}

        Const WeightColumnName As String = "TotalTags"
        Const TextColumnName As String = "Tag"
        Const NavigateUrlColumnName As String = "Tag"
        Const NavigateUrlFormatString As String = "ArticlesbyTag.aspx?ID={0}"

        Dim minWeight As Decimal = Decimal.MaxValue, maxWeight As Decimal = Decimal.MinValue

        For Each row As DataRowView In cloudData
            Dim numProductsObj As Object = row(WeightColumnName)
            If Not Convert.IsDBNull(numProductsObj) Then
                Dim numProductsDec As Decimal = Convert.ToDecimal(numProductsObj)

                If numProductsDec < minWeight Then minWeight = numProductsDec
                If numProductsDec > maxWeight Then maxWeight = numProductsDec
            End If
        Next

        Dim scaleUnitLength As Decimal = (maxWeight - minWeight + 1) / Convert.ToDecimal(FontScale.Length)

        For Each row As DataRowView In cloudData
            Dim numProductsObj As Object = row("TotalTags")
            If Not Convert.IsDBNull(numProductsObj) Then
                Dim numProductsDec As Decimal = Convert.ToDecimal(numProductsObj)

                Dim scaleValue As Integer = Math.Truncate((numProductsDec - minWeight) / scaleUnitLength)
                CloudMarkup.Text &= String.Format("<a href=""{0}"" style=""font-size:{1};"">{2}</a>{3}", _
                                            Page.ResolveUrl(String.Format(NavigateUrlFormatString, row(NavigateUrlColumnName).ToString())), FontScale(scaleValue), row(TextColumnName).ToString(), SpacerMarkup)
            End If
        Next


    End Sub
End Class
